<template>
  <div class="mainx">
    <!-- 面包屑到导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
      <!-- 添加按钮区域 -->
      <el-row class="btnBox">
        <el-col :span="2">
          <el-button type="primary" @click="addTypeData">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        ref="table"
        show-index
        border
        sum-text="sum"
        index-text="#"
        :data="goodsList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
        class="treeTable"
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.status === 'Y'"
            style="color: green"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="cat_level" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.levelNo === 1">一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.levelNo === 2"
          >二级
          </el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <template slot="operate" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editCateInfo(scope.row.serialNo)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeCateById(scope.row.serialNo)"
          ></el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.curPage"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    <!-- 添加分类弹窗 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addTypeDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form
        :model="addCateForm"
        ref="addCateFormRef"
        label-width="80px"
        :rules="addTypeFormrules"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addCateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectedCates"
            :options="parentList"
            :props="cascaderProps"
            @change="selectHandleChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
        <el-form-item label="排序号" prop="sortNo">
          <el-input v-model="addCateForm.sortNo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTypeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑商品分类信息的弹窗 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editCateDialogVisible"
      width="50%"
    >
      <el-form
        :model="editCateForm"
        ref="editCateFormRef2"
        label-width="80px"
        :rules="addTypeFormrules"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="editCateForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCateDialogClosed"
        >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品数据
      goodsList: [],
      // 查询参数
      queryInfo: {
        type: 3,
        curPage: 1,
        pagesSize: 5,
      },
      // 总数据
      total: 0,
      // 编辑商品分类信息的弹窗
      editCateDialogVisible: false,
      // 展示添加分类的对话框
      addTypeDialogVisible: false,
      // parentList 父级分类列表
      parentList: [],
      // 添加分类数据
      addCateForm: {
        name: '',
        // 父级分类id
        parentNo: null,
        // 分类等级 0一级分类，1二级分类以此类推
        levelNo: 1,
        sortNo: '0',
      },
      editCateForm: {
        serialNo: '',
        name: '',
      },
      // 添加分类名称的规则
      addTypeFormrules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'},
        ],
      },
      addTypeFormrules2: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'},
        ],
      },
      // 被选中的数据下拉框
      selectedCates: [],
      // 组件cascader的配置对象内容
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'serialNo',
        label: 'name',
        children: 'children',
      },
      columns: [
        {
          label: '分类名称',
          prop: 'name',
          width: '300px',
        },
        {
          label: '状态',
          type: 'template',
          template: 'isok',
        },
        {
          label: '序号',
          prop: 'sortNo',
        },
        {
          label: '图标',
          type: 'template',
          prop: 'icon',
        },
        {
          label: '商品数量',
          prop: 'goodsNumber',
        },
        {
          label: '分类编号',
          prop: 'serialNo',
        },
        {
          label: '分类级别',
          type: 'template',
          template: 'cat_level',
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate',
        },
      ],
    }
  },
  created() {
    this.getCategoryList()
  },
  watch: {
    editCateDialogVisible(newVal) {
      if (newVal) {
        this.queryCategoryTree()
      }
    },
    addTypeDialogVisible(newVal) {
      if (newVal) {
        this.queryCategoryTree()
      }
    },
  },
  methods: {
    // 监听pagesSize改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesSize = newSize
      this.getCategoryList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.curPage = newPage
      this.getCategoryList()
    },
    // 弹出添加分类的弹窗
    addTypeData() {
      this.addTypeDialogVisible = true
    },
    // 获取父级分类的数据列表
    getCategoryList() {
      this.$http({
        url: this.$http.adornUrl('/mall/categoryinfo/tree'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.queryInfo.curPage,
          'limit': this.queryInfo.pagesSize
        })
      }).then(({data}) => {
        console.log(data)
        if (data && data.code === 0) {
          this.goodsList = data.page.list
          this.total = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 监听下拉框选中事件
    selectHandleChange() {
      if (this.selectedCates.length > 0) {
        // 父级分类的id
        this.addCateForm.parentNo =
          this.selectedCates[this.selectedCates.length - 1]
        this.addCateForm.levelNo = this.selectedCates.length + 1
        return false
      } else {
        this.addCateForm.parentNo = 0
        this.addCateForm.levelNo = 1
      }
    },
    // 添项目分类
    addCate() {
      this.$http({
        url: this.$http.adornUrl('/mall/categoryinfo/save'),
        method: 'post',
        data: this.$http.adornData(this.addCateForm)
      }).then(({data}) => {
        console.log(data)
        if (data && data.code === 0) {
          this.$notify({
            title: '保存',
            type: 'success',
            message: '保存成功！'
          })
          this.getCategoryList()
          this.dataListLoading = false
        } else {
          this.$notify({
            title: '保存',
            type: 'error',
            message: '保存失败！'
          })
        }
      })
    },
    // 监听添加分类对话框关闭 重置表单数据
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedCates = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.addCateForm.name = ''
    },
    // 修改每个商品分类的信息
    editCateInfo(serialNo) {
      this.editCateDialogVisible = true
      this.$http({
        url: this.$http.adornUrl(`/mall/categoryinfo/info/${serialNo}`),
        method: 'get'
      }).then(({data}) => {
        console.log(data)
        if (data && data.code === 0) {
          this.editCateForm = data.categoryInfo
        }
      })
    },
    // 监听编辑分类信息对话框关闭的事件
    editCateDialogClosed() {
      this.$http({
        url: this.$http.adornUrl('/mall/categoryinfo/update'),
        method: 'post',
        data: this.$http.adornData(this.editCateForm)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$notify({
            title: '编辑',
            type: 'success',
            message: '编辑成功！'
          })
          this.getCategoryList()
          this.editCateDialogVisible = false
        } else {
          this.$notify({
            title: '编辑',
            type: 'error',
            message: '编辑失败！'
          })
        }
      })
    },
    // 删除商品分类
    removeCateById(serialNo) {
      console.log(serialNo)
      // 确认删除$confirm
      this.$http({
        url: this.$http.adornUrl('/mall/categoryinfo/delete'),
        method: 'post',
        data: this.$http.adornData([serialNo], false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$notify({
            title: '删除',
            type: 'success',
            message: '删除成功！'
          })
          this.getCategoryList()
        } else {
          this.$notify({
            title: '删除',
            type: 'error',
            message: '删除失败！'
          })
        }
      })
    },
    // 查询分类树图，用于选择父分类
    queryCategoryTree() {
      this.$http({
        url: this.$http.adornUrl('/mall/categoryinfo/tree'),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.parentList = data.page.list
        }
      })
    }
  }
}
</script>

<style lang="scss">
.mainx {
  .btnBox {
    margin: 20px 0 40px 0;
    display: flex;
    width: 100%;
    justify-content: flex-end;
  }}
</style>
